package GUI;

import 学生.Student;
import 数据库和网络.myDatebase;
import 试题和试卷.paper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;


public class rankPaperItem extends JFrame {

    JLabel num,paperLabel;
    JButton scoreButton,classRankButton;
    paper currentpaper;
    Student currentstu;


    public rankPaperItem(){}

    public rankPaperItem(int n, paper currentpaper){
        this.currentpaper=currentpaper;
        num = new JLabel(""+n);
        int loc ;
        if(n%5!=0){
            loc=n%5;
        }else {
            loc=5;
        }
        setTitleType(num);
        paperLabel = new JLabel("试卷名："+currentpaper.getName());
        /*先暂且只写了试卷名*/
        setTextType(paperLabel);
        num.setBounds(80,120+50*loc,60,50);
        paperLabel.setBounds(140,120+50*loc,600,50);
        scoreButton = new JButton("试卷得分");
        classRankButton = new JButton("分数分布");
        setButtonType(scoreButton);
        setButtonType(classRankButton);
        scoreButton.setBounds(740,120+50*loc,80,50);
        classRankButton.setBounds(820,120+50*loc,80,50);
        scoreButton.addActionListener(new scoreListener());
        classRankButton.addActionListener(new classRankListener());
    }

    public rankPaperItem(int n, paper currentpaper,Student currentStu){
        this.currentstu=currentStu;
        this.currentpaper=currentpaper;
        num = new JLabel(""+n);
        int loc ;
        if(n%5!=0){
            loc=n%5;
        }else {
            loc=5;
        }
        setTitleType(num);
        paperLabel = new JLabel("试卷名："+currentpaper.getName());
        /*先暂且只写了试卷名*/
        setTextType(paperLabel);
        num.setBounds(80,120+50*loc,60,50);
        paperLabel.setBounds(140,120+50*loc,600,50);
        scoreButton = new JButton("试卷得分");
        classRankButton = new JButton("分数分布");
        setButtonType(scoreButton);
        setButtonType(classRankButton);
        scoreButton.setBounds(740,120+50*loc,80,50);
        classRankButton.setBounds(820,120+50*loc,80,50);
        scoreButton.addActionListener(new scoreListenerWithStu());
        classRankButton.addActionListener(new classRankListenerWithStu());
    }
    public void setTitleType(JLabel label){
        Font bigFont = new Font("sanserif",Font.PLAIN,16);
        label.setBackground(Color.lightGray);
        label.setOpaque(true);
        label.setFont(bigFont);
        label.setBorder(BorderFactory.createLineBorder(Color.darkGray));
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }
    public void setTextType(JLabel label){
        Font smallFont = new Font("sanserif",Font.PLAIN,12);
        label.setBackground(Color.white);
        label.setOpaque(true);
        label.setFont(smallFont);
        label.setBorder(BorderFactory.createLineBorder(Color.lightGray));
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }
    public void setButtonType(JButton button){
        Font smallFont = new Font("sanserif",Font.PLAIN,14);
        //button.setBackground(Color.GRAY);
        button.setFont(smallFont);
        button.setBorder(BorderFactory.createLineBorder(Color.darkGray));
        button.setHorizontalAlignment(SwingConstants.CENTER);
    }

    public void add(){
    }

    public class scoreListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            int sel=0,def=0,fill=0,sub=0,total=0;

            myDatebase rankSeeker=new myDatebase("result_record");
            String sql=String.format("select * from result_record."+currentpaper.getName()+" where result_record.%s.studentID=%s",
                    currentpaper.getName(),enterWindow.currentStudent.getSchoolID());
            try {
                rankSeeker.statement.execute(sql);
                rankSeeker.resultSet=rankSeeker.statement.executeQuery(sql);
                while(rankSeeker.resultSet.next()){
                    sel=rankSeeker.resultSet.getInt("selection_score");
                    def=rankSeeker.resultSet.getInt("definition_score");
                    fill=rankSeeker.resultSet.getInt("fillingblank_score");
                    sub=rankSeeker.resultSet.getInt("subjective_score");
                    total=sel+def+fill+sub;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            if(total==0){
                JFrame notYet=new JFrame();
                JPanel panel = new JPanel(new GridLayout(4, 1,20,20));
                JLabel successLab=new JLabel();
                JLabel lab2=new JLabel(" ");
                notYet.add(panel);
                notYet.add(successLab);
                notYet.add(lab2);

                notYet.setSize(200,150);
                Font bigFont = new Font("sanserif",Font.PLAIN,15);

                String a="您还未完成该考试！！！";
                successLab.setText(a);
                successLab.setFont(bigFont);
                panel.setSize(notYet.getSize());
                panel.add(lab2);
                panel.add(successLab);
                notYet.getContentPane().add(BorderLayout.CENTER,panel);
                notYet.setLayout(new FlowLayout());//流式布局
                notYet.setLocationRelativeTo(null);//居中

                notYet.setTitle("在线考试平台");
                notYet.setVisible(true);
                //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }else{
                scoreWindow demo = new scoreWindow(sel,def);
                demo.lab4.setText("主观题得分为:"+(sub+fill));
                demo.lab5.setText("总分为："+(demo.totalScore+sub+fill));
                demo.mainPanel.updateUI();
                demo.setVisible(true);
                demo.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            }


        }
    }

    public class scoreListenerWithStu implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            int sel=0,def=0,fill=0,sub=0,total=0;

            myDatebase rankSeeker=new myDatebase("result_record");
            String sql=String.format("select * from result_record."+currentpaper.getName()+" where result_record.%s.studentID=%s",
                    currentpaper.getName(),currentstu.getSchoolID());
            try {
                rankSeeker.statement.execute(sql);
                rankSeeker.resultSet=rankSeeker.statement.executeQuery(sql);
                while(rankSeeker.resultSet.next()){
                    sel=rankSeeker.resultSet.getInt("selection_score");
                    def=rankSeeker.resultSet.getInt("definition_score");
                    fill=rankSeeker.resultSet.getInt("fillingblank_score");
                    sub=rankSeeker.resultSet.getInt("subjective_score");
                    total=sel+def+fill+sub;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            /*boolean empty=false;
            String [][]resultSet=new String[2][currentpaper.getQuestionNumber()];
            myDatebase saveDatebase=new myDatebase("result_record");
            try {
                sql=String.format("select * from result_record.%s where studentID=%s",
                        currentpaper.getName(),enterWindow.currentStudent.getSchoolID());
                saveDatebase.statement.execute(sql);
                saveDatebase.resultSet=saveDatebase.statement.executeQuery(sql);
                while(saveDatebase.resultSet.next()){
                    for(int i=1;i<=currentpaper.getQuestionNumber();i++){
                        resultSet[1][i-1]=saveDatebase.resultSet.getString(resultSet[0][i-1]+"_current_answer");
                    }

                }


            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally{
                saveDatebase.close();
            }

            for(int i=0;i<currentpaper.getQuestionNumber();i++){
                if(resultSet[1][i].isEmpty())
                { empty=true;
                    break;
                }

            }*/
            if(total==0){
                //if(total==0||empty){
                JFrame notYet=new JFrame();
                JPanel panel = new JPanel(new GridLayout(4, 1,20,20));
                JLabel successLab=new JLabel();
                JLabel lab2=new JLabel(" ");
                notYet.add(panel);
                notYet.add(successLab);
                notYet.add(lab2);

                notYet.setSize(200,150);
                Font bigFont = new Font("sanserif",Font.PLAIN,15);

                String a="该学生未完成该考试！！！";
                successLab.setText(a);
                successLab.setFont(bigFont);
                panel.setSize(notYet.getSize());
                panel.add(lab2);
                panel.add(successLab);
                notYet.getContentPane().add(BorderLayout.CENTER,panel);
                notYet.setLayout(new FlowLayout());//流式布局
                notYet.setLocationRelativeTo(null);//居中

                notYet.setTitle("在线考试平台");
                notYet.setVisible(true);
                //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }else{
                scoreWindow demo = new scoreWindow(sel,def);
                demo.lab4.setText("主观题得分为:"+(sub+fill));
                demo.lab5.setText("总分为："+(demo.totalScore+sub+fill));

                demo.mainPanel.updateUI();
                demo.setVisible(true);
                demo.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            }


        }
    }

    public class classRankListener implements  ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            ArrayList<Student> classMember=new ArrayList<>();
            myDatebase felloCompare=new myDatebase("result_record");
            myDatebase classConfirm=new myDatebase("student");
            String sql="select * from student.student ";
            try {
                classConfirm.statement.execute(sql);
                classConfirm.resultSet=classConfirm.statement.executeQuery(sql);
                while (classConfirm.resultSet.next()){
                    if(classConfirm.resultSet.getString(4)!=null&&classConfirm.resultSet.getString(4).equals(enterWindow.currentStudent.getMyClass()))
                    {classMember.add(new Student(classConfirm.resultSet.getString("StudentID"))); }
                }

                Iterator<Student> nameIte=classMember.iterator();
                int i=0,def=0,sel=0,fill=0,sub=0;
                while(nameIte.hasNext()){
                    Student currstu= nameIte.next();
                    sql=String.format("select * from result_record."+currentpaper.getName()+" where result_record.%s.StudentID=%S"
                    ,currentpaper.getName(),classMember.get(i++).getSchoolID());
                    /*sql="select *from result_record.demopaper where result_record.demopaper.studentID=1";*/
                    felloCompare.statement.execute(sql);
                    felloCompare.resultSet=felloCompare.statement.executeQuery(sql);
                    while(felloCompare.resultSet.next()){
                        def=felloCompare.resultSet.getInt("definition_score");
                        sel=felloCompare.resultSet.getInt("selection_score");
                        fill=felloCompare.resultSet.getInt("fillingblank_score");
                        sub=felloCompare.resultSet.getInt("subjective_score");
                    }currstu.setInstantScore(def+sel+fill+sub);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            felloCompare.close();
            classConfirm.close();

            int NumBelow60=0,Num60=0,Num70=0,Num80=0,Num90=0;

                for(int i=0;i<classMember.toArray().length;i++){

                    System.out.println(classMember.get(i).getSchoolID()+classMember.get(i).getInstantScore());
                    int sco = classMember.get(i).getInstantScore();
                    if (sco < 60) {
                        NumBelow60++;
                    } else if (sco < 70) {
                        Num60++;
                    } else if (sco < 80) {
                        Num70++;
                    } else if (sco < 90) {
                        Num80++;
                    } else {
                        Num90++;
                    }
                }

                new classRankChart(currentpaper.getName(),NumBelow60,Num60,Num70,Num80,Num90);

        }
    }

    public class classRankListenerWithStu implements  ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            ArrayList<Student> classMember=new ArrayList<>();
            myDatebase felloCompare=new myDatebase("result_record");
            myDatebase classConfirm=new myDatebase("student");
            String sql="select * from student.student ";
            try {
                classConfirm.statement.execute(sql);
                classConfirm.resultSet=classConfirm.statement.executeQuery(sql);
                while (classConfirm.resultSet.next()){
                    if(classConfirm.resultSet.getString(4)!=null&&classConfirm.resultSet.getString(4).equals(currentstu.getMyClass()))
                    {classMember.add(new Student(classConfirm.resultSet.getString("StudentID"))); }
                }

                Iterator<Student> nameIte=classMember.iterator();
                int i=0,def=0,sel=0,fill=0,sub=0;
                while(nameIte.hasNext()){
                    Student currstu= nameIte.next();
                    sql=String.format("select * from result_record."+currentpaper.getName()+" where result_record.%s.StudentID=%S"
                            ,currentpaper.getName(),classMember.get(i++).getSchoolID());
                    /*sql="select *from result_record.demopaper where result_record.demopaper.studentID=1";*/
                    felloCompare.statement.execute(sql);
                    felloCompare.resultSet=felloCompare.statement.executeQuery(sql);
                    while(felloCompare.resultSet.next()){
                        def=felloCompare.resultSet.getInt("definition_score");
                        sel=felloCompare.resultSet.getInt("selection_score");
                        fill=felloCompare.resultSet.getInt("fillingblank_score");
                        sub=felloCompare.resultSet.getInt("subjective_score");
                    }currstu.setInstantScore(def+sel+fill+sub);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            felloCompare.close();
            classConfirm.close();

            int NumBelow60=0,Num60=0,Num70=0,Num80=0,Num90=0;

            for(int i=0;i<classMember.toArray().length;i++){

                System.out.println(classMember.get(i).getSchoolID()+classMember.get(i).getInstantScore());
                int sco = classMember.get(i).getInstantScore();
                if (sco < 60) {
                    NumBelow60++;
                } else if (sco < 70) {
                    Num60++;
                } else if (sco < 80) {
                    Num70++;
                } else if (sco < 90) {
                    Num80++;
                } else {
                    Num90++;
                }
            }

            new classRankChart(currentpaper.getName(),NumBelow60,Num60,Num70,Num80,Num90);

        }
    }
}


